package com.sduept.bigdata.iec61850.client;

import com.beanit.iec61850bean.*;

import java.io.IOException;
import java.net.InetAddress;
import java.util.Collection;

/**
 * @author: lxj
 * @date: 2022/3/24 8:17
 * @since
 */
public class ClientDemo {
    static int serverPort = 102;
    static String serverAddress = "10.140.21.243";
    public static void main(String[] args) throws Exception {
        ClientSap clientSap = new ClientSap();
        InetAddress address = InetAddress.getByName(serverAddress);
        String authenticationParameter = null;
        ClientAssociation clientAssociation = clientSap.associate(address, serverPort, null, new ClientEventListener() {
            @Override
            public void newReport(Report report) {
                System.out.println("获取报告："+report.toString());
            }

            @Override
            public void associationClosed(IOException e) {

            }
        });
        System.out.println("start retrieveModel ");
        long t1 = System.currentTimeMillis();
        ServerModel model =  clientAssociation.retrieveModel();
        long t2 = System.currentTimeMillis();
        System.out.println("retrieveModel user time: "+(t2-t1)/1000);
//        clientAssociation.enableReporting();

        Collection<Brcb> brcbs = model.getBrcbs();
        for (Brcb b : brcbs){
            clientAssociation.enableReporting(b);
            System.out.println(b.getReference().toString()+"  "+b.getName());
        }
        Collection<Urcb> urcbs = model.getUrcbs();
        for (Urcb b : urcbs){
            clientAssociation.enableReporting(b);
            System.out.println(b.getReference().toString()+"  "+b.getName());
        }
//        Collection<DataSet> datasets = model.getDataSets();
//        for (DataSet ds : datasets){
//            System.out.println(ds.toString());
//        }

//        List<FileInformation> fileInformations =  clientAssociation.getFileDirectory("SCD");
//        for (FileInformation f : fileInformations){
//            System.out.println(f.getFilename()+" "+ f.getFileSize());
//        }
//        List<FileInformation> fileInformations2 =  clientAssociation.getFileDirectory("COMTRADE");
//        for (FileInformation f : fileInformations2){
//            System.out.println(f.getFilename()+" "+ f.getFileSize());
//
//        }
//        File file = new File("E:\\公司文档\\项目管理\\智能录波器\\子站smcd文件\\山大\\smcdmodel.zip");
//        clientAssociation.getFile("/SCD/smcdmodel.zip", new GetFileListener() {
//            @Override
//            public boolean dataReceived(byte[] fileData, boolean moreFollows) {
//                System.out.println("file data size:"+ fileData.length);
//                try {
//                    FileUtils.writeByteArrayToFile(file,fileData,true);
//                } catch (IOException e) {
//                    throw new RuntimeException(e);
//                }
//                return true;
//            }
//        });

//        printSortedByFC(model);
//        BdaInt32 daNode = (BdaInt32) model.findModelNode(
//				new ObjectReference("SampleIEDDevice1/DGEN1.GnOpSt.stVal"),
//				Fc.ST);
//		daNode.setValue(2);
//        clientAssociation.setDataValues(daNode);
//
//        BdaVisibleString desc = (BdaVisibleString) model.findModelNode(
//				new ObjectReference("SampleIEDDevice1/MMXU2.NamPlt.d"),
//                Fc.DC);
//		desc.setValue("test test");
//        clientAssociation.setDataValues(desc);
//
//		ModelNode dobj = model.findModelNode(new ObjectReference("SampleIEDDevice1/LPHD1.PhyNam.vendor"), Fc.DC);
//		clientAssociation.getDataValues((FcModelNode)dobj);
//        printRecursive(dobj);
//
//		ModelNode dobj2 = model.findModelNode(new ObjectReference("SampleIEDDevice1/MMXU1.NamPlt"), Fc.DC);
//		clientAssociation.getDataValues((FcModelNode)dobj2);
//        printRecursive(dobj2);
		System.out.println("Client is done. Will quit.");
    }
    public static void printSortedByFC(ServerModel server) {
        System.out.println(server.toString());
        for (ModelNode ld : server) {
            System.out.println(ld.toString());
            for (ModelNode ln : ld) {
                System.out.println(ln.toString());
                LogicalNode logicalNode = (LogicalNode) ln;
                Collection<ModelNode> mclildren = logicalNode.getChildren();
                for (ModelNode modelNode : mclildren) {
                        printRecursive(modelNode);
                }
            }
        }
    }

    private static void printRecursive(ModelNode modelNode) {
        System.out.println(modelNode.toString());
        if (modelNode.getChildren() != null) {
            for (ModelNode modelNodeChild : modelNode.getChildren()) {
                printRecursive(modelNodeChild);
            }
        }
    }

}
